const mysql = require('mysql2/promise');
require('dotenv').config();

async function testConnection() {
  console.log('🔍 测试数据库连接...');
  console.log('📊 数据库配置:');
  console.log(`   Host: ${process.env.DB_HOST}`);
  console.log(`   Port: ${process.env.DB_PORT}`);
  console.log(`   Database: ${process.env.DB_DATABASE}`);
  console.log(`   Username: ${process.env.DB_USERNAME}`);
  
  try {
    const connection = await mysql.createConnection({
      host: process.env.DB_HOST,
      port: process.env.DB_PORT,
      user: process.env.DB_USERNAME,
      password: process.env.DB_PASSWORD,
      database: process.env.DB_DATABASE,
    });

    console.log('✅ 数据库连接成功！');

    // 检查表是否存在
    const [tables] = await connection.execute('SHOW TABLES');
    console.log('📋 现有表:');
    tables.forEach(table => {
      console.log(`   - ${Object.values(table)[0]}`);
    });

    // 检查users表是否存在
    const [usersTable] = await connection.execute("SHOW TABLES LIKE 'users'");
    if (usersTable.length === 0) {
      console.log('❌ users表不存在，需要初始化数据库');
      console.log('💡 请运行: mysql -h 49.51.193.79 -u prompt -p prompt < init-remote-database.sql');
    } else {
      console.log('✅ users表存在');
    }

    await connection.end();
  } catch (error) {
    console.error('❌ 数据库连接失败:', error.message);
    console.log('💡 请检查数据库配置和网络连接');
  }
}

testConnection();

